package com.atguigu.dao.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.pojo.User;

import java.util.List;

public class UserDaoImpl extends baseDao<User> implements UserDao {
    @Override
    public void insert(User user) {
        String sql = "insert into t_user(username, password, email) values(?, ?, ?) ";
        update(sql, user.getUsername(), user.getPassword(), user.getEmail());
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from t_user where id = ?";
        update(sql, id);
    }

    @Override
    public void update(User user) {
        String sql = "update t_user set username = ?, password = ?, email = ? where id = ?";
        update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getId());
    }

    @Override
    public User getUserById(int id) {
        String sql = "select id, username, password, email from t_user where id = ?";
        return getInstance(sql, id);
    }

    @Override
    public List<User> getUserAll() {
        String sql = "select id, username, password, email from t_user";
        return getForList(sql);
    }

    @Override
    public long getCount() {
        String sql = "select count(*) from t_user";
        return getValue(sql);
    }

    @Override
    public Integer getUserByName(String username) {
        String sql = "select id from t_user where username=?";
        return getInstance(sql, username).getId();
    }
}
